import { user_sign_staticNode } from './cla_user_sign_staticNode';
import { user_sign_eventBind } from './cla_user_sign_eventBind';
import {
  attri_user_signed,
  attri_user_signed_val,
  spacename_user_sign,
  user_signed_login_duration,
} from '../DataDefine/sourceData';
import type { user_sign_props } from './cla_user_sign_props';
import { listenEvent_mana } from '../../../../global_comps/Classes/listenEvent_mana/Classes/cla_listenEvent_mana';
import { api_port } from '../../../../global_comps/api_config';
import { code_create_account } from './api/Types';
import { settimeNth_anima_check } from '../../../../../global/Func/settimeNth';
import { inject_remove_doc_per_delay } from '../../../../global_comps/DataDefine/sourceData';
import { inject_chi, remove_chi } from '../../../../global_comps/Func';
import { inject_dom_func_promise } from '../../../../global_comps/Func/inject_dom';
import { remove_class } from '../../../../global_comps/Func/remove_class';
import {
  css_child_margin_top,
  css_cursor_pointer,
} from '../../../../global_comps/Css';
import { sleep } from '../../../../../global/Func/sleep';
import { high_light_dom } from '../../../../global_comps/Func/high_light_dom';
import { remove_inp_val } from '../../../../global_comps/Func/remove_inp_val';
import { set_localstorage_login_user_identity } from '../../../../global_comps/api_config/localstorage_login_user_identity';
import { res_user_sign_info } from './api/cla_res_user_sign_info';
import {
  prop_user_passworld,
  req_user_sign_info,
} from './api/cla_req_user_sign_info';

const user_sign_dataManaTag = 'user_sign_dataMana';

export class user_sign_dataMana extends user_sign_staticNode {
  get [Symbol.toStringTag]() {
    return user_sign_dataManaTag;
  }

  constructor(props: user_sign_props) {
    super(props);
  }

  /**
   * 事件 绑定 this
   */
  eventBind = new user_sign_eventBind();

  /**
   * 事件 监听管理
   */
  eventM = new listenEvent_mana(spacename_user_sign);

  /**
   * 检测 通过
   */
  check_over = false;

  /**
   * 注册 信息返回 结果
   */
  res_info: res_user_sign_info = undefined as any;

  /**
   * 信息集
   */
  weakmap_info = new WeakMap<HTMLInputElement, any>();

  /**
   * 移除 本体 点击 跳转
   */
  freeze_border_click() {
    this.eventM.freeze(this.dom_border, 'click', this.eventBind.signed_login);
  }

  /**
   * 添加 本体 点击 跳转
   */
  unfreeze_border_click() {
    this.eventM.unfreeze(this.dom_border, 'click', this.eventBind.signed_login);
  }

  /**
   * 装载 信息
   */
  set_cur_dom_val(dom: HTMLInputElement) {
    this.weakmap_info.set(dom, dom.value);
  }

  /**
   * 获取 信息
   */
  get_cur_dom_val(dom: HTMLInputElement) {
    return this.weakmap_info.get(dom);
  }

  /**
   * 获取 注册 信息
   */
  get_all_sign_info() {
    let ret = new req_user_sign_info();
    ret.name = this.get_cur_dom_val(this.inp_name);
    ret.Identity = this.get_cur_dom_val(this.inp_identity);
    ret.phone = this.get_cur_dom_val(this.inp_phone);
    ret[prop_user_passworld] = this.get_cur_dom_val(this.inp_passworld);

    return ret;
  }

  /**
   * 测定 两个 passworld 是否相同
   */
  check_passworld_is_same() {
    let pass = this.get_cur_dom_val(this.inp_passworld);

    let pass_certain = this.get_cur_dom_val(this.inp_passworld_certain);

    this.check_over = pass == pass_certain;

    return this.check_over;
  }

  /**
   * 密码 不相同
   */
  passworld_is_not_same() {
    console.log('密码 不相同');
  }

  /**
   * 用户 身份重新 被注册 时
   *
   * 移除 所有 input 元素 值
   */
  #inject_identity_operate() {
    // 移除 所有 inp 值
    let remova_val_arr = [
      this.inp_identity,
      // this.inp_name,
      this.inp_passworld,
      this.inp_passworld_certain,
      this.inp_phone,
    ];

    for (const cur_dom of remova_val_arr) {
      remove_inp_val(cur_dom);
    }

    // 高亮 身份证 号 元素
    high_light_dom(this.inp_identity.parentNode as any);
  }

  /**
   * 用户 身份重新 被注册 时
   *
   * 移除 手机号 input 元素 值
   */
  #inject_phone_operate() {
    // 移除 所有 inp 值
    let remova_val_arr = [
      // this.inp_identity,
      // this.inp_name,
      // this.inp_passworld,
      // this.inp_passworld_certain,
      this.inp_phone,
    ];
    for (const cur_dom of remova_val_arr) {
      remove_inp_val(cur_dom);
    }

    // 高亮 手机 号 元素
    high_light_dom(this.inp_phone.parentNode as any);
  }

  /**
   * 接受 返回结果
   *
   * 执行 动画
   */
  async #res_anima() {
    // 注册 成功
    if (this.res_info.res_code == code_create_account.access) {
      this.#signed_login();
      return;
    }

    await this.#inject_sign_dom();

    if (this.res_info.res_code == code_create_account.identity_registered) {
      this.#inject_identity_operate();
      return;
    }

    if (this.res_info.res_code == code_create_account.phone_registered) {
      this.#inject_phone_operate();
      return;
    }
  }

  /**
   * 将 注册 成功
   *
   * 用户 唯一 身份 进行 记录
   */
  set_localstorage_cur_user_identity() {
    set_localstorage_login_user_identity(this.res_info.security_id);
  }

  /**
   * 移除
   *
   * sign 注册 信息表
   */
  #remove_sign_dom() {
    let p_arr = [];

    let cur_i = 0;

    let cur_dom: HTMLElement;

    /**
     * 忽略 移出 项
     */
    let ignore_remove_arr = [this.dom_login];

    for (cur_dom of this.dom_border.children as any) {
      if (ignore_remove_arr.includes(cur_dom)) {
        continue;
      }
      cur_i++;
      let p = new Promise<void>((resolve) => {
        settimeNth_anima_check(
          this.dom_border,
          cur_i * inject_remove_doc_per_delay,
          remove_chi(cur_dom, resolve)
        );
      });

      p_arr.push(p);
    }

    return Promise.all(p_arr);
  }

  /**
   * 注册 失败
   *
   * 回退 注册表 doc
   */
  #inject_sign_dom() {
    let p_arr = [];

    let cur_i = 0;

    let cur_dom: HTMLElement;

    /**
     * 忽略 移出 项
     */
    let ignore_inject_arr = [this.dom_login];

    for (cur_dom of this.dom_border.children as any) {
      if (ignore_inject_arr.includes(cur_dom)) {
        continue;
      }
      cur_i++;
      let p = new Promise<void>((resolve) => {
        settimeNth_anima_check(
          this.dom_border,
          cur_i * inject_remove_doc_per_delay,
          inject_chi(cur_dom, resolve)
        );
      });

      p_arr.push(p);
    }

    return Promise.all(p_arr);
  }

  /**
   * 注册成功
   *
   * 注入 登录
   */
  async #signed_login() {
    // 移除 子 节点 top
    remove_class(this.dom_border, css_child_margin_top);

    // 添加 手指
    remove_class(this.dom_border, css_cursor_pointer, true);

    // 更改 top 50%
    this.dom_border.setAttribute(attri_user_signed, attri_user_signed_val);

    await sleep(user_signed_login_duration - 100);

    await inject_dom_func_promise(this.dom_login);

    this.unfreeze_border_click();
  }

  /**
   * 提交 注册 信息
   *
   * 获取 返回 结果
   */
  async #api_sign() {
    let sign_info = this.get_all_sign_info();
    // console.log('提交了', sign_info);

    let res = await api_port.post('/my_schedule/user/create_acount', sign_info);

    this.res_info = new res_user_sign_info(res.data);
  }

  /**
   * 提交 注册信息
   */
  async submit_info() {
    if (!this.check_over) {
      console.log('有提示 错误');
      return;
    }

    // 同步 进行
    let p_arr = [this.#remove_sign_dom(), this.#api_sign()];

    await Promise.all(p_arr);

    this.#res_anima();
  }
}
